
import { AREA, FIELDS, UISTATUS, BUTTONID, CARDTABLEAREAIDS } from '../../constance';
import { buttonControl } from '../viewControl/buttonControl';
import { headAfterEvent } from '../afterEvents';

/**
 * 清空卡片数据
 * @param {*} props 
 */
function clearCardData(props) {
        props.form.EmptyAllFormValue(AREA.cardFormId);
        CARDTABLEAREAIDS.forEach((areaId) => {
                props.cardTable.setTableData(areaId, { rows: [] });
        });
}

/**
 * 当主组织为空时，设置页面
 * @param {*} props 
 */
function setCardPageForEmptypk_org(props) {
        props.form.setFormItemsDisabled(AREA.cardFormId, { pk_org: false });
        props.initMetaByPkorg(FIELDS.pk_org);
        props.button.setDisabled({
                        [BUTTONID.cardBody_1_AddLine]: true,
                [BUTTONID.cardBody_2_AddLine]: true,

                [BUTTONID.cardBody_1_DeleteLine]: true,
                [BUTTONID.cardBody_2_DeleteLine]: true,

                [BUTTONID.cardBody_1_CopyLine]: true,
                [BUTTONID.cardBody_2_CopyLine]: true
        });
}

/**
 * 设置卡片界面：数据、按钮状态
 * @param {*} props 
 * @param {*} data 
 * @param {*} isCompare 差异比较
 */
function setCardPage(props, data, isCompare = true) {
        props.beforeUpdatePage();
        if (data.head && data.head[AREA.cardFormId]) {
                props.form.setAllFormValue({ [AREA.cardFormId]: data.head[AREA.cardFormId] });
        }
        if (data.bodys) {
                CARDTABLEAREAIDS.forEach((areaId) => {
                        if (data.bodys[areaId]) {
                                isCompare
                                        ? (data.bodys[areaId] = props.cardTable.updateDataByRowId(areaId, data.bodys[areaId], true))
                                        : props.cardTable.setTableData(areaId, data.bodys[areaId], null, true, true);
                                props.cardTable.selectAllRows(areaId, false); // 取消表体勾选
                        } else {
                                props.cardTable.setTableData(areaId, { rows: [] });
                        }
                });
        }

        // 更新按钮状态
        buttonControl.call(this, props);
        props.updatePage(AREA.cardFormId, CARDTABLEAREAIDS);
}

/**
 * 默认主组织
 * @param {*} props 
 * @param {*} context 
 */
function mainOrgInit(props, context) {
        let pk_org = context.pk_org;
        if (pk_org) {
                props.form.setFormItemsDisabled(AREA.cardFormId, { pk_org: false });
                headAfterEvent.call(this, props, AREA.cardFormId, FIELDS.pk_org, { value: pk_org });
        } else {
        }
        setCardPageForEmptypk_org.call(this, props);
}

/**
 * 根据模板获取对应的区域ID和按钮ID
 * @param {*} moduleId 模板ID
 * @param {*} status 页面状态
 * @param {*} copyData 复制行数据
 */
function getAreaAndBtnId(moduleId, status, copyData) {
        // let bodyAreaId, InnerAreaId, InnerBtnIds;
        const result = CARDTABLEAREAIDS.map((item, i)=>{
                if(moduleId===item){
                        return {

                                bodyAreaId: AREA[`cardBody_${i+1}_shoulder`],
                                InnerAreaId: AREA[`cardBody_${i+1}_inner`],
                                InnerBtnIds: status == UISTATUS.browse? null: copyData ? [BUTTONID[`cardBody_${i+1}_PasteLine`]] : [BUTTONID[`cardBody_${i+1}_DeleteLine`], BUTTONID[`cardBody_${i+1}_InsertLine`]],
                        };
                }

        }).filter(item => item !== undefined);
        console.log(result[0], '88888888888888888')
        return result && result[0];
        // return { bodyAreaId: bodyAreaId, InnerAreaId: InnerAreaId, InnerBtnIds: InnerBtnIds };
}

export { setCardPageForEmptypk_org, mainOrgInit, clearCardData, setCardPage, getAreaAndBtnId};
